package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.risetechnology.data.Recipe;

public class ChooseRecipeDialog extends Dialog {
	private ArrayList<Recipe> recipes, choosenRecipes;
	private ListView mList;
	private Button mBtnOk;
	SelectedListener listener;

	public ChooseRecipeDialog(Context context, ArrayList<Recipe> recipes,
			ArrayList<Recipe> choosenRecipes, SelectedListener listener) {
		super(context);
		this.listener = listener;
		this.recipes = recipes;
		this.choosenRecipes = (ArrayList<Recipe>) choosenRecipes.clone();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_choose_recipe);
		mList = (ListView) findViewById(R.id.list_recipe);
		mBtnOk = (Button) findViewById(R.id.btn_ok);
		mBtnOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					dismiss();
					Collections.sort(choosenRecipes, new Comparator<Recipe>() {

						@Override
						public int compare(Recipe lhs, Recipe rhs) {
							return lhs.getName().compareTo(rhs.getName());
						}
					});
					listener.onSelected(choosenRecipes);
				}
			}
		});
		mList.setAdapter(new RecipeAdapter(getContext(), recipes));
	}

	public interface SelectedListener {
		public void onSelected(ArrayList<Recipe> choosenRecipe);
	}

	public class RecipeAdapter extends ArrayAdapter<Recipe> {

		public RecipeAdapter(Context context, List<Recipe> objects) {
			super(context, R.layout.layout_recipe_item, objects);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			final ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.layout_recipe_item, parent, false);
				holder = new ViewHolder();
				holder.tvRecipe = (TextView) convertView
						.findViewById(R.id.tv_recipe);
				holder.cbRecipe = (CheckBox) convertView
						.findViewById(R.id.cb_recipe);
				holder.checked = choosenRecipes.indexOf(getItem(position)) >= 0;
				holder.listener = new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							if (choosenRecipes.indexOf(getItem(position)) == -1)
								choosenRecipes.add(getItem(position));
						} else {
							choosenRecipes.remove(getItem(position));
						}
					}
				};
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Recipe recipe = getItem(position);
			holder.tvRecipe.setText(recipe.getName());
			holder.cbRecipe.setOnCheckedChangeListener(holder.listener);
			holder.cbRecipe.setChecked(holder.checked);
			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					holder.cbRecipe.performClick();
				}
			});
			return convertView;
		}

		private class ViewHolder {
			TextView tvRecipe;
			CheckBox cbRecipe;
			boolean checked;
			CompoundButton.OnCheckedChangeListener listener;
		}

	}
}
